<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index()
    {
        //推荐
        $tuijs = M('gy_xm')->where(['is_online'=>1,'is_tj'=>1,'is_over'=>1])->order('time desc')->limit(4)->select();
        $this->assign('tuijs',$tuijs);

    	$gyxm = I('get.gyxm');
    	if ($gyxm) {
    		$map['xm_name'] = array('like',$gyxm."%");
	    	$xms = M('gy_xm')->where($map)->select();
	    	$this->assign('xms',$xms);
    	}else {
	    	//项目
	    	$xms = M('gy_xm')->where(['is_online'=>1,'is_over'=>1])->order('time desc')->limit(5)->select();
	    	$this->assign('xms',$xms);
    	}
    	
        $this->display();
    }

    //项目列表
    public function xms()
    {
        //所有在线项目
        $xms = M('gy_xm')->where(['is_online'=>1,'is_over'=>1])->select();
        $this->assign('xms',$xms);

        $this->display();
    }
    //项目详细
    public function xm()
    {
    	$id = I('get.id');

        //检测该项目是否有发起过
        $checkfq = M('gy_faqi')->where(['xm_id'=>$id,'uid'=>$_SESSION['id']])->find();
        $this->assign('checkfq',$checkfq);
    	//详细信息
    	$xm_info = M('gy_xm')->where(array('id'=>$id))->find();
    	$this->assign('xm_info',$xm_info);
        //一起捐多少
        $fq_id = M('gy_faqi')->where(['xm_id'=>$id])->getField('id');
        $yq_num = M('gy_wuping')->where(['xm_id'=>$xm_info['id'],'fq_id'=>$fq_id])->sum('num');
        $yq_num = $yq_num?$yq_num:'0';
        $this->assign('yq_num',$yq_num);

        //慈善发起显示
        $ciss_count = M('gy_faqi')->where(['xm_id'=>$id,'fq_sf'=>4])->count();
        $this->assign('ciss_count',$ciss_count);
        $ciss = M('gy_faqi')->field('gy_faqi.school,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>4])->limit(4)->select();
        $this->assign('ciss',$ciss);
        //企业发起显示
        $qiyes_count = M('gy_faqi')->where(['xm_id'=>$id,'fq_sf'=>3])->count();
        $this->assign('qiyes_count',$qiyes_count);
        $qiyes = M('gy_faqi')->field('gy_faqi.school,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>3])->limit(4)->select();
        $this->assign('qiyes',$qiyes);
        //学校发起显示
        $xxs_count = M('gy_faqi')->where(['xm_id'=>$id,'fq_sf'=>2])->count();
        $this->assign('xxs_count',$xxs_count);
        $xxs = M('gy_faqi')->field('gy_faqi.school,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>2])->limit(4)->select();
        $this->assign('xxs',$xxs);
        //个人发起显示
        $gers_count = M('gy_faqi')->where(['xm_id'=>$id,'fq_sf'=>1])->count();
        $this->assign('gers_count',$gers_count);
        $gers = M('gy_faqi')->field('gy_faqi.name,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>1])->limit(4)->select();
        $this->assign('gers',$gers);

    	$this->display();
    }
    //爱心留言
    public function msg()
    {
    	if (IS_POST) {
    		$data['xm_id'] = I('post.xm_id');
    		$data['name'] = I('post.names');
    		$data['phone'] = I('post.phone');
    		$data['msg'] = I('post.msg');
    		$data['time'] = time();

            if (empty($data['name']) && empty($data['phone'])) {
                $url = U('user/login');
                $arr = array(
                    'error' => 0,
                    'info' => '要登陆才能评论哦',
                    'url' => $url
                    );
                echo json_encode($arr);
                exit;
            }
    		$res = M('gy_comm')->add($data);
    		if ($res) {
    			// $this->redirect('index/msg', array('id' => $data['xm_id']));
                $arr = array(
                    'error' => 1,
                    'info' => '评论成功',
                    );
                echo json_encode($arr);
    		}else {
                $arr = array(
                    'error' => 0,
                    'info' => '评论失败'
                    );
                echo json_encode($arr);
    		}

    	}else {

	    	$id = I('get.id');
	    	$this->assign('id',$id);


            //详细信息
            $xm_info = M('gy_xm')->where(array('id'=>$id))->find();
            $this->assign('xm_info',$xm_info);

	    	//详细信息
	    	$comms = M('gy_comm')->where(array('xm_id'=>$id))->order('time desc')->limit(10)->select();
	    	$this->assign('comms',$comms);

            //公共用户信息
            $userinfo = M('gy_user')->where(['id'=>$_SESSION['id']])->find();
            $this->assign('userinfo',$userinfo);
	    	
	    	$this->display();
    	}
    }
    //捐赠记录
    public function record()
    {
        $id = I('get.id');
        $xz = I('get.xz')?I('get.xz'):'1';
        $this->assign('xz',$xz);

        //检测该项目是否有发起过
        $checkfq = M('gy_faqi')->where(['xm_id'=>$id,'uid'=>$_SESSION['id']])->find();
        $this->assign('checkfq',$checkfq);
        //详细信息
        $xm_info = M('gy_xm')->where(array('id'=>$id))->find();
        $this->assign('xm_info',$xm_info);

        //xz
        if ($xz == 2) {
            //学校
            $gers = M('gy_faqi')->field('gy_faqi.id,gy_faqi.xm_id,gy_faqi.school,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>2])->select();
        }elseif ($xz == 3) {
            //企业
            $gers = M('gy_faqi')->field('gy_faqi.id,gy_faqi.xm_id,gy_faqi.school,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>3])->select();
        }elseif ($xz == 4) {
            $gers = M('gy_faqi')->field('gy_faqi.id,gy_faqi.xm_id,gy_faqi.school,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>4])->select();
        }else {
            //个人
            $gers = M('gy_faqi')->field('gy_faqi.id,gy_faqi.xm_id,gy_faqi.name,t2.face')->join('gy_user t2 ON gy_faqi.uid=t2.id')->where(['xm_id'=>$id,'fq_sf'=>1])->select();
        }
        $this->assign('gers',$gers);

        $this->display();
    }
    //常见问题
    public function problems()
    {
        $id = I('get.id');

        //检测该项目是否有发起过
        $checkfq = M('gy_faqi')->where(['xm_id'=>$id,'uid'=>$_SESSION['id']])->find();
        $this->assign('checkfq',$checkfq);
        //详细信息
        $xm_info = M('gy_xm')->where(array('id'=>$id))->find();
        $this->assign('xm_info',$xm_info);

        $probs = D('problems')->probs();
        $this->assign('probs',$probs);
        $this->display();
    }

    //捐物
    public function jw()
    {
    	if (IS_POST) {
    		$data['xm_id'] = I('post.xm_id');
            $data['name'] = I('post.name');
            $data['phone'] = I('post.phone');

            $data['fq_id'] = I('post.fq_id');
            $data['uid'] = $_SESSION['id']?$_SESSION['id']:0;

            $send_to = I('post.yzm');

            $data['wp_name'] = I('post.wp_name');
			$data['num'] = I('post.num');
            $data['is_fanshi'] = I('post.is_fanshi');
            $data['address'] = I('post.address');

            $data['jop_type'] = I('post.jop_type');
            $data['beizhu'] = I('post.beizhu');

            $data['time'] = time();

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //物品不空
            if (empty($data['wp_name'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '至少要捐赠一个物品'
                    );
                echo json_encode($arr);
                exit;
            }

            if (!ctype_digit($data['num'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入数字'
                    );
                echo json_encode($arr);
                exit;
            }

            if ($data['is_fanshi'] == 1) {
                if (empty($data['address'])) {
                    $arr = array(
                        'error' => 0,
                        'info' => '请输入收书地址'
                        );
                    echo json_encode($arr);
                    exit;
                }

            }
            //入库
            $res = M('gy_wuping')->add($data);
            if ($res) {

            	//捐赠成功 xm_num 自动加上数量
            	M('gy_xm')->where(array('id'=>$data['xm_id']))->setInc('xm_num',$data['num']);
                //fq_id存在 fq_num 自动加上数量
                if (!empty($data['fq_id'])) {
                    M('gy_faqi')->where(array('id'=>$data['fq_id']))->setInc('fq_num',$data['num']);
                }

                $url = U('index/xm',array('id'=>$data['xm_id']));
                $arr = array(
                    'error' => 1,
                    'info' => '预约成功,请保持手机畅通',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '预约失败,麻烦重新提交信息'
                    );
                echo json_encode($arr);
            }

    	}else {
    		//选中
    		$xm_id = I('get.xm_id');
    		$this->assign('xm_id',$xm_id);
            //发起
            $fq_id = I('get.fq_id');
            $this->assign('fq_id',$fq_id);

    		//项目
	    	$xms = M('gy_xm')->select();
	    	$this->assign('xms',$xms);

    		$this->display();
    	}  	
    }

    //捐款
    public function jk()
    {
        $this->display();
    }

    //秒滴短信发送开始----------------------
    public function send_to()
    {
        if (IS_POST) {
            $to = I('post.to');
             //手机
             $preg_js = "/^1[34578][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $to);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
             //发送
            miaodi($to);
            $arr = array(
                'error' => 1,
                'info' => '正在发送...',
                );
            echo json_encode($arr); 
        }
    }
















}